package soen6441;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.IOException;

class SearchDir {
    public static void main(String[] args) throws IOException, ParseException {
        File indexDir = new File(args[0]);
        String queryString = args[1];

        Directory fsDir = FSDirectory.open(indexDir);
        IndexSearcher is = new IndexSearcher(fsDir, true);

        QueryParser queryParser = new QueryParser(Version.LUCENE_CURRENT,
                "contents",
                new StandardAnalyzer(Version.LUCENE_CURRENT)  );

        Query query = queryParser.parse(queryString);

        TopScoreDocCollector collector =
                TopScoreDocCollector.create(10, true);

        is.search(query, collector);
        ScoreDoc[] hits = collector.topDocs().scoreDocs;

        for ( ScoreDoc scoreDoc : hits ){
            Document d = is.doc(scoreDoc.doc);
            System.out.println( scoreDoc.score + " " + d.get("path") );
        }
    }
}
